const app = getApp();
const utils = require('../../utils/util.js');
var wxpay = require('../../utils/pay.js');
Component({
  /**
   * 右侧导航栏
   * 组件的属性列表
   */
  properties: {
    showmenu: {
      type: Boolean,
      value: false
    },
    showmsg: {
      type: Boolean,
      value: false
    },
    msglength: {
      type: Number,
      value: 0
    },
    logincode: {
      type: String,
      value: ''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    showmenu: false,
    showmenuAll: false, //是否展开菜单
    currPagePath: '',
    cardId: 0,
    showEmpover: false,
    showAi: false,
    cardInfo: {},
    myUserInfo: {},
    userInfo: {},
    animationData: "",
    versionid: 4,
    payDialog: { //代支付弹框
      isshow: false,
      ispay: false,
      title1: "",
      title2: "",
      submittext: "",
    },
    x: 370,
    y: 140,
    screenHeight: 500,
    isAccredit: false,
    canIUseGetUserProfile: false
  },

  ready: function () {
    let that = this;
    if (app.globalData.userInfo) {
      that.getAicardInfo();
      that.getStaffIdByUid();
      that.setData({
        myUserInfo: app.globalData.myUserInfo,
        userInfo: app.globalData.userInfo
      })
    }
    var info = wx.getSystemInfoSync();
    var screenHeight = info.screenHeight - 150;
    let screenWidth = info.screenWidth;
    that.setData({
      screenHeight: screenHeight,
      x: screenWidth
    })
    utils.isAccredit(function (isAccredit) {
      that.setData({
        isAccredit: isAccredit
      })
    });
    if (wx.getUserProfile) {
      that.setData({
        canIUseGetUserProfile: true
      })
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getAicardInfo: function () {
      let cardId = wx.getStorageSync('cardId');
      let that = this;
      if (cardId) {
        wx.request({
          url: app.globalData.host + '/api/BusinessCard/GetAiCard?staffId=' + cardId + '&uid=' + app.globalData.userInfo.Uid,
          success: function (res) {
            if (res.data.Code == 0) {
              that.setData({
                cardInfo: res.data.Data.CardInfo
              })
            }
          }
        })
      }
    },
    //判断是否创建过名片
    getStaffIdByUid: function () {
      var that = this;
      wx.request({
        url: app.globalData.host + '/api/Staff/GetStaffIdByUid?Uid=' + app.globalData.userInfo.Uid,
        success: function (res) {
          if (res.data.Code == 0) {
            if (res.data.Data) {
              that.setData({
                showAi: true
              })
              wx.setStorageSync("staffid", res.data.Data.Id)
            } else {
              that.setData({
                showAi: false
              })
              wx.removeStorageSync("staffid")
            }
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.Message,
              showCancel: false,
            })
          }
        },
      })
    },
    // 改变悬浮菜单显示状态
    changeRightFloat: function () {
      this.setData({
        showmenuAll: !this.data.showmenuAll
      })
    },
    // 点击授权管理：
    toEmpover: function () {
      this.setData({
        showEmpover: !this.data.showEmpover
      })
    },
    // 查看最近访问的名片列表
    formSubmitCardList: function (e) {
      if (e.detail.iv) {
        utils.AuthorizeUserInfoOld(e, this.data.logincode);
        wx.redirectTo({
          url: '/pages/weijieke/cardList/index'
        })
      }
    },
    toCardList: function () {
      wx.redirectTo({
        url: '/pages/weijieke/cardList/index'
      })
    },
    getUserProfile: function () {
      let that = this;
      wx.getUserProfile({
        desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
        success: (res) => {
          console.log('用于完善会员资料', res);
          utils.AuthorizeUserInfo(res, that.data.loginCode, function (flag) {
            if (flag) {
              wx.redirectTo({
                url: '/pages/weijieke/cardList/index'
              })
            } else {//注册失败，重新点击授权
              wx.login({
                success: function (res) {
                  that.data.loginCode = res.code;
                }
              })
            }
          });
        },
        fail: (err) => {
          wx.showToast({
            title: '用户拒绝授权',
            icon: 'none'
          })
        }
      })
    },
    //玖玖客平台
    toPlatfrom: function () {
      wx.reLaunch({
        url: '/pages/indexDefault/index',
      })
    },
    // 消息通知
    toMsgNotify: function () {
      wx.navigateTo({
        url: '/pages/msgNotify/index',
      })
    },
    // 老板是否授权所有操作给当前用户
    storeManager: function () {
      let that = this;
      let cardInfo = that.data.cardInfo;
      if (cardInfo) {
        wx.request({
          url: app.globalData.host + '/api/Staff/GetStaffExtendList?staffId=' + cardInfo.Id + '&keyword=25',
          success: function (res) {
            if (res.data.Code == 0) {
              let authorizeList = res.data.Data;
              let hasAuthorize = false;
              if (authorizeList.length > 0) {
                for (let i = 0; i < authorizeList.length; i++) {
                  if (app.globalData.myUserInfo.Uid == authorizeList[i].Value) {
                    app.globalData.userInfo.Uid = cardInfo.Uid;
                    hasAuthorize = true;
                    wx.setStorageSync('staffid', cardInfo.Id);
                    that.setData({
                      userInfo: app.globalData.userInfo,
                      myUserInfo: app.globalData.myUserInfo,
                      showAi: true
                    })
                    wx.showToast({
                      title: '授权成功',
                      mask: true,
                      duration: 1500
                    })
                    wx.getStorageSync("staffid", cardInfo.Id);
                    break;
                  }
                }
                if (!hasAuthorize) {
                  wx.showToast({
                    title: '商家未授权',
                    mask: true,
                    duration: 1500
                  })
                }
              } else {
                wx.showToast({
                  title: '商家未授权',
                  mask: true,
                  duration: 1500
                })
              }
            }
          }
        })
      }
    },
    // 退出店长管理
    exitStoreManager: function () {
      let that = this;
      app.globalData.userInfo.Uid = app.globalData.myUserInfo.Uid;
      that.getStaffIdByUid();
      that.setData({
        userInfo: app.globalData.userInfo,
        showAi: false
      })
      wx.showToast({
        title: '退出成功',
        mask: true,
        duration: 1500
      })
    },
    //查看我的名片
    lookMyCardInfo: function () {
      var staffid = wx.getStorageSync("staffid");
      wx.reLaunch({
        url: '/pages/index/index?id=' + staffid + '&source=2',
      })
    },
    // 注册玖玖客授权手机号。点击创建我的名片按钮
    getMobiles: function (e) {
      let that = this;
      utils.getMobile(e, function (mobiles) {
        if (mobiles) {
          that.toCreatMyCardInfo();
        } else {
          wx.showToast({
            title: '请授权手机号',
            mask: true
          })
        }
      })
    },
    //创建我的名片
    toCreatMyCardInfo: function () {
      let {
        payDialog
      } = this.data;
      payDialog.isshow = true;
      if (app.globalData.platform == 'ios') {
        payDialog.ispay = false;
        payDialog.title1 = "您还不是会员";
        payDialog.submittext = "发送创建申请";

      } else {
        payDialog.ispay = true,
          payDialog.title1 = "您还不是会员";
        payDialog.submittext = "立即创建";
      }
      this.setData({
        payDialog: payDialog
      })
    },
    // 创建名片时1选择共享推广版4智慧经营版
    versionSelect: function (e) {
      let versionid = e.currentTarget.dataset.versonid;
      let payDialog = this.data.payDialog;
      if (versionid == 1) {
        payDialog.submittext = '立即注册';
      }
      if (versionid == 4) {
        if (app.globalData.platform == 'ios') {
          payDialog.submittext = "发送创建申请";
          if (this.data.showAi) {
            payDialog.submittext = "发送续费申请"
          }
        } else {
          payDialog.submittext = "立即创建";
          if (this.data.showAi) {
            payDialog.submittext = "立即续费";
          }
        }
      }
      this.setData({
        versionid: versionid,
        payDialog: payDialog
      })
    },
    //创建支付安卓手机,苹果手机发送申请(先授权微信头像昵称)
    creatMyCardPay: function (e) {
      let that = this;
      if (e.detail.iv) {
        utils.AuthorizeUserInfoOld(e, that.data.logincode);
        that.toCreatMyCardPay();
      } else {
        wx.showToast({
          title: '请授权微信头像、昵称',
          mask: true
        })
      }
    },
    //创建支付安卓手机,苹果手机发送申请(先授权微信头像昵称)===用户授权api修改
    creatMyCardPayByfile: function () {
      let that = this;
      wx.getUserProfile({
        desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
        success: (res) => {
          console.log('用于完善会员资料', res);
          utils.AuthorizeUserInfo(res, '', function (flag) {
            if (flag) {
              that.toCreatMyCardPay();
            } else {//注册失败，重新点击授权
              wx.login({
                success: function (res) {
                  that.data.loginCode = res.code;
                }
              })
            }
          });
        },
        fail: (err) => {
          wx.showToast({
            title: '请授权微信头像、昵称',
            mask: true
          })
        }
      })
    },
    //创建支付安卓手机,苹果手机发送申请
    toCreatMyCardPay: function () {
      var that = this;
      var cardInfo = that.data.cardInfo;
      if (that.data.versionid == 3) {
        that.createStaffPayRequest(that.data.versionid);
      } else {
        if (app.globalData.platform != 'ios' || that.data.versionid == 1) {
          wxpay.createStaffOrder(app, cardInfo.Id, that.data.versionid);
          utils.aiCardActionRecord(24);
        } else {
          //发送创建申请
          that.createStaffPayRequest(that.data.versionid);
        }
      }
    },
    //申请代付
    applyCardPay: function (e) {
      var that = this;
      if (e.detail.iv) {
        utils.AuthorizeUserInfoOld(e, that.data.logincode);
        that.createStaffPayRequest(that.data.versionid);
      } else {
        wx.showToast({
          title: '请授权微信头像、昵称',
          mask: true
        })
      }
    },
    //申请代付===用户授权api修改
    applyCardPayByfile: function (e) {
      var that = this;
      wx.getUserProfile({
        desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
        success: (res) => {
          console.log('用于完善会员资料', res);
          utils.AuthorizeUserInfo(res, '', function (flag) {
            if (flag) {
              that.createStaffPayRequest(that.data.versionid);
            } else {//注册失败，重新点击授权
              wx.login({
                success: function (res) {
                  that.data.loginCode = res.code;
                }
              })
            }
          });
        },
        fail: (err) => {
          wx.showToast({
            title: '请授权微信头像、昵称',
            mask: true
          })
        }
      })
    },
    //申请代付
    createStaffPayRequest: function (versionid) {
      let that = this;
      var cardInfo = that.data.cardInfo;
      wx.showLoading({
        title: '申请中',
        mask: true
      })
      wx.request({
        url: app.globalData.host + '/api/Staff/CreateStaffPayRequest?fromUid=' + app.globalData.userInfo.Uid + '&toUid=' + cardInfo.Uid + '&industryCode=' + versionid,
        success: function (res) {
          wx.hideLoading();
          if (res.data.Code == 0) {
            wx.showToast({
              title: '申请成功!'
            })
            that.dialogsState();
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.Message,
              showCancel: false
            })
          }
        }
      })
    },
    //关闭显示弹框
    dialogsState: function () {
      let {
        payDialog
      } = this.data;
      if (payDialog.isshow) {
        payDialog.isshow = false;
      } else {
        payDialog.isshow = true;
      }
      this.setData({
        payDialog: payDialog
      })
    },
    //进入聊天页面
    formSubmit: function (e) {
      let that = this;
      let nickname = that.data.cardInfo.Name;
      wx.request({
        url: app.globalData.host + '/api/IM/UserByOpenId?openId=' + app.globalData.userInfo.Uid,
        success: function (res) {
          if (res.data.Code == 0 && res.data.Data) {
            let fromuid = res.data.Data.Id;
            wx.request({
              url: app.globalData.host + '/api/IM/UserByOpenId?openId=' + that.data.cardInfo.Uid,
              success: function (res2) {
                if (res2.data.Code == 0 && res2.data.Data) {
                  let touid = res2.data.Data.Id;
                  let chatparam = 'touid=' + touid + '&fromuid=' + fromuid + '&nickname=' + nickname + '&storeid=' + app.globalData.storeId;
                  wx.navigateTo({
                    url: '/pages/h5page/index?url=sessionDetail.html&chatparam=' + encodeURIComponent(chatparam),
                  })
                }
              }
            })
          }
        }
      })
      utils.aiCardActionRecord(18);
    },
  }

})